<template>
  <basic-container>
    <basic-header title="用户购买历史" />
    <FilterTable :option="filterOption" :showBtn="false" @handle-filter="handleFilter" />
    <avue-crud :page="pageOption" :option="tableOption" :data="tableData" :table-loading="loading" @current-change="handlePageTo" @size-change="handleSizeChange"></avue-crud>
  </basic-container>
</template>
<script>
import actionTypes from "@/const/action";
import cloumnsOptionMap from "@/const/columns";
import filterOptionMap from "@/const/filters";
import FilterTable from "@/components/FilterTable";
import { SyncGetTableData } from "@/api/table";

// 7.14 待调
const DEFAULT_TABLE_OPTIONS = {
  refreshBtn: false,
  columnBtn: false,
  addBtn: false,
  maxHeight: 560,
  border: false,
  menu: false,
  align: "center",
  editBtn: false,
  delBtn: false,
  header: false
};

export default {
  name: "first",
  data() {
    return {
      action: actionTypes.USER_CORE_HISTORY,
      formPath: "",
      pageOption: {
        total: 0, //总页数
        currentPage: 1, //当前页数
        pageSize: 10 //每页显示多少条
      },
      tableData: [],
      loading: false,
      filterData: {}
    };
  },
  created() {
    if (!this.$route.params.id) {
      this.$router.go(-1);
      return;
    }
    this.tableOption = {
      ...DEFAULT_TABLE_OPTIONS,
      column: cloumnsOptionMap[this.action] || []
    };
    this.filterOption = filterOptionMap[this.action] || [];
    this.getPageData();
  },
  methods: {
    async getPageData() {
      this.loading = true;
      const { data, error } = await SyncGetTableData(this.action, {
        pageIndex: this.pageOption.currentPage,
        pageSize: this.pageOption.pageSize,
        id: Number(this.$route.params.id),
        ...this.filterData
      });
      if (error) {
        this.$message({
          type: "error",
          message: error
        });
      } else {
        this.setPageOption({
          total: data.count
        });
        this.tableData = data.bussData;
      }
      this.loading = false;
    },
    setPageOption(opt) {
      this.pageOption = Object.assign({}, this.pageOption, opt);
    },
    handlePageTo(num) {
      this.setPageOption({
        currentPage: num
      });
      this.getPageData();
    },
    handleSizeChange(size) {
      this.setPageOption({
        currentPage: 1,
        pageSize: size
      });
      this.getPageData();
    },
    handleFilter(data) {
      this.filterData = data;
      this.setPageOption({
        currentPage: 1
      });
      this.getPageData();
    }
  },
  components: { FilterTable }
};
</script>


